package manager.dao;

import manager.domain.CardInfo;

import java.util.ArrayList;

public class OtherCardDao implements BaseCardDao {
    //        创建学生数组
    private static ArrayList<CardInfo> cardInfos =new ArrayList<>();

    static {
//        要求卡号长度5，第一位是小写字母，后四位是数字，充值金额最少20
        CardInfo cardInfo1 =new CardInfo("a1234","Luke","password","20");
        cardInfos.add(cardInfo1);
    }

    public boolean addCard(CardInfo cardInfo) {
        cardInfos.add(cardInfo);
        return true;
    }

    public CardInfo[] findAllCard() {
        CardInfo[] cards1=new CardInfo[cardInfos.size()];
        for (int i = 0; i < cards1.length; i++) {
            cards1[i]= cardInfos.get(i);
        }
        return cards1;
    }

    public void deleteCardById(String delId) {
//        查找ID在容器中的索引位置
        int index=getIndex(delId);
//        将该位置用null覆盖
        cardInfos.remove(index);
    }

    public int getIndex(String id) {
        int index=-1;
        for (int i = 0; i < cardInfos.size(); i++) {
            CardInfo cardInfo = cardInfos.get(i);
            if(cardInfo !=null&& cardInfo.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;
    }

    public void updateCard(String updateId, CardInfo newcard) {
//        查找ID所在索引位置
        int index=getIndex(updateId);
//        该索引位置替换
        cardInfos.set(index,newcard);
    }
}
